<template>
	<view class="sss" style=",z-index: -2;position: absolute;">
		<!-- <view class="" style="width: 100%;height: 100%;"> -->
		<image src="../../static/infobj.png" mode="" style="width: 100%;height: 100%;z-index: -1;position: absolute;">
		</image>
		<!-- </view> -->

		<view class="" style="z-index: 1;">

			<view class="" style="display: flex;flex-direction: column;padding-left: 20px;padding-top: 24px;">
				<text style="font-size: 17px;color: #333333;font-weight: 500;">最高可借（元）</text>
				<text style="font-size: 39px;color: #4A5DFF;font-weight: bold;margin-top: 10px;">200000</text>
				<view class="" style="margin-top: 10px;">
					<text style="font-size: 14px;color: #4A5DFF;">填写下方资产，资产</text>
					<text style="font-size: 14px;color: #D82B0E;">越多</text>
					<text style="font-size: 14px;color: #4A5DFF;">借款额度</text>
					<text style="font-size: 14px;color: #D82B0E;">越高</text>
					<text style="font-size: 14px;color: #4A5DFF;">哦</text>
				</view>

			</view>

			<view class="" style="background-color: #FFFFFF; margin-top: 18x;margin: 20px;border-radius: 6px;">
				<view class=""
					style="display: flex;flex-direction: row;justify-content: space-between;height: 50px;align-items: center;padding-left: 16px;padding-right: 16px;">
					<text>姓名</text>
					<input type="text" value="" @input="onKeyNameInput" v-model="username" placeholder="请输入您的姓名"
						style="text-align: end;" />
				</view>
				<view class="" style="background-color: #E4E4E4;height: 1px;margin-left:  15px;margin-right: 15px;" />

				<view class=""
					style="display: flex;flex-direction: row;justify-content: space-between;height: 50px;align-items: center;padding-left: 16px;padding-right: 16px;">
					<text>身份证号</text>
					<input type="text" value="" v-model="idCard" placeholder="请输入您的身份证号" style="text-align: end;" />
				</view>
			</view>

			<view class="" style=" margin-left: 20px;margin-right: 20px;border-radius: 6px;background-color: #FFFFFF;">
				<pickers @address="address" @close="close">

					<view class=""
						style="display: flex; justify-content: space-between;background-color: #FFFFFF;height: 44px;align-items: center;padding-left: 16px;padding-right: 16px;;border-radius: 6px;">
						<text>工作所在地</text>
						<text style="color: #333333;font-size: 14px;">{{addressTitle}}</text>
					</view>
				</pickers>

				<view class="" style="background-color: #E4E4E4;height: 1px;margin-left:  15px;margin-right: 15px;" />

				<view class="" v-for="(model,index) in danxuanBtn">
					<view class="" style="margin-top: 10px;margin-left: 16px;margin-bottom: 12px;">
						<text>{{model.name}}</text>
					</view>

					<view class="filter-body-section-body style-flex style-flex-wrap" style="padding-left: 25px;">
						<axb-check-box ref="radio3" :list="model.itemList" @change="radioChangeType"></axb-check-box>
					</view>

				</view>

				<!-- <view class="" style="margin-top: 10px;margin-left: 16px;margin-bottom: 12px;">
					<text>个人资产（多选）</text>
				</view>
				<view class="filter-body-section-body style-flex style-flex-wrap" style="padding-left: 25px;">
					<axb-check-box ref="radio1" :multi="true" :list="assets" @change="radioChangeStatus">
					</axb-check-box>
				</view> -->
			</view>






		</view>

		<view class="" @click="postInfo()"
			style="display: flex;flex-direction: row;justify-content: center;margin-top: 10px;">
			<image src="../../static/infobtn.png" mode="" style="width: 355px; height: 85px;"></image>
		</view>
		
		<view class=""
			style="display: flex;flex-direction: row; flex-wrap: wrap; align-items: center;justify-content: center;margin-bottom: 20px;" >
			<image :src="infoCheckbtn ? '../../static/check.png' :'../../static/indexbtnunsel.png'" style="width: 14px;height: 14px;margin-right: 10px;" mode=""  @click="checkbtn()">
			</image>
			<text style="font-size: 12px;color: #999999;" @click="checkbtn()">我已阅读并同意</text>
			<text style="color: #999999; font-size: 12px;" @click="goToPage('\\static/protocol/个人信息共享授权协议.html')">《个人信息共享授权协议》</text>
			<text style="color: #999999;font-size: 12px;" @click="goToPage('\\static/protocol/用户注册服务协议.html')">《用户注册服务协议》</text>
		</view>
		

		<view class="" @click="postInfo()" style="display: flex;flex-direction: row;justify-content: center; margin-bottom: 40rpx;">
			<text style="font-size: 12px;color: #999999;">国家级数据加密，仅用于贷款</text>
		</view>
		
		<protocol ref="protocolRef"> </protocol>
		
		</view>
	</view>
</template>

<script>
	import axbCheckBox from '../../components/axb-checkbox/axb-checkbox.vue'
	import protocol from './protocol.vue'
	import pickers from "@/components/ming-picker/ming-picker.vue"
	import qs from 'qs'
	import CryptoJS from 'crypto-js/crypto-js'
	
	export default {
		data() {
			return {
				danxuanBtn: [],
				selDanxuan: {},
				assets: [],
				username: '',
				idCard: '',
				loanAmount: '',
				zhimafenValue: '',
				width: 0,
				height: 0,
				addressTitle: '请选择城市',
				chooseAssets: false,
				provinceCode: '',
				cityCode: '',
				infoCheckbtn: true,
				showProtocol: false
			}
		},
		onLoad() {
			this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight
			
			this.getInfo()

		},
		onReady() {
			let systemInfo = uni.getSystemInfoSync()
			this.width = systemInfo.windowWidth
			this.height = systemInfo.windowHeight

		},

		methods: {
			goToPage(path){
				// #ifdef H5
				window.location.href = path
				// #endif
			},
			checkbtn() {
				this.infoCheckbtn = !this.infoCheckbtn
			},
			address(e) {
				this.addressTitle = e.value.join('-')

				this.provinceCode = e.code[0]
				this.cityCode = e.code[1]
			},
			

			radioChangeType(model) {
				// console.log(val) // 单选时 返回选中项的value, 反选返回null
				// this.zhimafenValue = val
				// console.log(model)
				if (model.includes('null')) {
					var s = model.substring(4, model.length)
					var ss = s.split('-')[0]
					this.$delete(this.selDanxuan, ss)
				} else {
					var obj = {}; //或者 var obj=new Object();
					var key = model.split('-')[0]
					var value = model.split('-')[1]
					obj[key] = value;
					Object.assign(this.selDanxuan, obj)
				}
				// console.log(this.selDanxuan)
			},
			radioChangeStatus(val) {
				// console.log(val) // 多选时 返回选中项的value数组, 反选返回[]
				if (val[0] == 'clean') {
					//改变check 值
					this.assets.forEach((e) => {

						e.check = 0


					})
					this.chooseAssets = true


				} else {
					//改变check 值
					this.assets.forEach((e) => {
						val.forEach((value) => {
							if (e.value == value) {
								e.check = 1
							}
						})

					})


				}





			},
			cleanAssets() {

				this.assets.forEach((e) => {
					// console.log(e)
					if (e.name != '以上都没有') {
						e.checked = 1;
					}
				})
			},
			async getInfo() {
				uni.showLoading({
					title: '加载中...'
				})
				try{
					const res = await this.$myRequest({
						url: 'api/member/getBaseInfo',
						method: 'post',
						data: {
					
						}
					});
					let ss = res.multDataList
					ss.forEach((e) => {
					
						e.itemList.forEach((eson) => {
					
							let zhima = Object.assign(eson, {
								'check': 0
							})
							// eson.push(zhima)
							eson.value = e.value + '-' + eson.value
						})
					
						this.danxuanBtn.push(e)
					
					})
					// console.log(this.danxuanBtn)
					let assetsArr = res.singleDataList || []
					assetsArr.forEach((e) => {
						let zhima = Object.assign(e, {
							'check': 0
						})
						this.assets.push(zhima)
					})
					let nothing = {
						'name': '以上都没有',
						'value': 'clean',
						'check': 0
					}
					this.assets.push(nothing)
				} catch(e){
					console.error(e)
				}
				uni.hideLoading()

			},
			//限制只能输入汉字
			onKeyNameInput: function(event) {
				var value = event.target.value;



				if (!value || value == " ") {
					return '';
				}
				const rule = /[^\u4E00-\u9FA5]/g;
				this.$nextTick(function() {

					this.username = value.replace(rule, '');
					// console.log(this.name)



				})

			},
			async postInfo() {
				let infoMap = {}

				Object.assign(infoMap, {
					'username': this.username
				})
				Object.assign(infoMap, {
					'idCard': this.idCard
				})

				// Object.assign(infoMap, {
				// 	'zhimaScore': this.zhimafenValue
				// })
				Object.assign(infoMap, {
					'address': this.addressTitle
				})
				this.assets.forEach((e) => {
					if (e.check == 1) {
						this.chooseAssets = true;
					}
					if (e.value != 'clean') {
						var obj = {}; //或者 var obj=new Object();
						var key = e.value;
						var value = e.check
						obj[key] = value;

						Object.assign(infoMap, obj)
					}
				})
				Object.assign(infoMap, this.selDanxuan)
				if (this.username == '' || this.idCard == '') {
					uni.showToast({
						title: '请完善身份信息',
						icon: 'error'
					})
					return;
				}
				if (this.idCard.length != 18) {
					uni.showToast({
						title: '请输入正确的身份证信息',
						icon: 'error'
					})
					return;
				}

				if (this.addressTitle == '请选择城市') {
					uni.showToast({
						title: '请选择城市',
						icon: 'error'
					})
					return;
				}

				if (Object.keys(this.selDanxuan).length != this.danxuanBtn.length) {
					uni.showToast({
						title: '请完善相关资料',
						icon: 'error'
					})
					return;
				}
				// if (this.chooseAssets == false) {
				// 	uni.showToast({
				// 		title: '请选择资产',
				// 		icon: 'error'
				// 	})
				// 	return;
				// }
				Object.assign(infoMap, {
					'provinceCode': this.provinceCode
				})
				Object.assign(infoMap, {
					'cityCode': this.cityCode
				})
				uni.showLoading({
					title: '借款中...'
				})
				try{
					const resGetUserInfo = await this.$myRequest({
						url: 'api/member/getUserInfo',
						method: 'post',
						data: {
						}
					});
					const tokenMobile = resGetUserInfo.mobile;
					if (tokenMobile.length !== 11) {
						uni.setStorageSync('token_mobile', "");
						uni.setStorageSync('token', '')
						uni.setStorageSync('islogin', false)
						const browserParameterStr = sessionStorage.getItem('url_param')
						const browserParameter = browserParameterStr ? JSON.parse(browserParameterStr) : {}
						delete browserParameter.uid;
						delete browserParameter.u;
						sessionStorage.setItem('url_param', JSON.stringify(browserParameter))
						uni.redirectTo({
							url: '/pages/index/index/?' + qs.stringify({...browserParameter, uid: undefined })
						})
						// #ifdef H5
						window.location.href = `/#/?${qs.stringify(browserParameter)}`
						// #endif
						return;
					}
					// console.log(infoMap)
					
					const res = await this.$myRequest({
						url: 'api/member/updateInfo',
						method: 'post',
						data: infoMap,
					});
					uni.hideLoading()
					if (res.newNumber == true) {
						if (res.spePushResut.pushSuccess == true) {
							var ss = res.spePushResut.skipUrl
							if (ss) {
								uni.setStorageSync('authorize', true)
								uni.setStorageSync('authorizeUrl', ss)
							}
						}
					
					}
					console.log(res)
					// if (res.spePushResut) {
					// 	return this.$refs.protocolRef.showModal(res.spePushResut)
					// }
					if (res.spePushResut && res.spePushResut.pushSuccess == true) {
						// return this.$refs.protocolRef.showModal(res.spePushResut, infoMap);
						const spePushResut = res.spePushResut
						const dataTemp = {
							orderNo: spePushResut.pushOrderNo,
							logoUrl: spePushResut.companyLogo,
							mainTitle: spePushResut.infoName,
							compantName: spePushResut.companyName,
							minRate: spePushResut.minRate,
							protocolUrl: spePushResut.protocolUrl,
							applyName: infoMap.username,
							applyIdCard: infoMap.idCard,
						}
						const dataUrl = CryptoJS.enc.Base64url.stringify(CryptoJS.enc.Utf8.parse(JSON.stringify(dataTemp)))
						window.open(`\\approve/#/protocol?data=${dataUrl}`, "_top")
						return;
					}
					
					
					if (res.spePushResut && res.spePushResut.companyName) {
						// uni.redirectTo({
						// 	url: `../result/result?companyName=${res.spePushResut.companyName}`
						// })
						window.open('\\approve/#/evaluateAuth?isToProdList=1', "_top")
					} else {
						// uni.redirectTo({
						// 	url: `../result/result`
						// })
						window.open('\\approve/#/evaluateAuth?isToProdList=1', "_top")
					}
					
				}catch(e){
					//TODO handle the exception
					console.log(e)
					uni.hideLoading()
				}
			}


		},
		components: {
			axbCheckBox,
			pickers,
			protocol
		},
	}
</script>

<style>

</style>
